package com.graduation.springbootinit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.common.ResultUtils;
import com.graduation.springbootinit.model.entity.ShopPart.ProductComment;
import com.graduation.springbootinit.model.entity.User;
import com.graduation.springbootinit.service.ProductCommentService;
import com.graduation.springbootinit.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/product/comment")
public class ProductCommentController {

    @Resource
    private ProductCommentService productCommentService;


    @Resource
    private UserService userService;

    @PostMapping("/add")
    public BaseResponse<Boolean> addComment(@RequestBody ProductComment comment, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        comment.setUserId(user.getId());
        comment.setUserName(user.getUserName());
        comment.setUserAvatar(user.getUserAvatar());
        
        // 处理图片列表
        if (comment.getImages() != null && !comment.getImages().isEmpty()) {
            // 将逗号分隔的图片字符串转换为List
            List<String> imageList = Arrays.asList(comment.getImages().split(","));
            // 设置imageList，这会自动将List转换为JSON字符串并存入images字段
            comment.setImageList(imageList);
        }
        
        productCommentService.addComment(comment);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteComment(@RequestBody ProductComment comment) {
        productCommentService.deleteComment(comment.getId(), comment.getUserId());
        return ResultUtils.success(true);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateComment(@RequestBody ProductComment comment) {
        productCommentService.updateComment(comment);
        return ResultUtils.success(true);
    }

    @GetMapping("/list")
    public BaseResponse<Page<ProductComment>> getProductComments(
            @RequestParam Long productId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        Page<ProductComment> comments = productCommentService.getProductComments(productId, pageNum, pageSize);
        return ResultUtils.success(comments);
    }

    @GetMapping("/list/all")
    public BaseResponse<List<ProductComment>> getAllProductComments(@RequestParam Long productId) {
        List<ProductComment> comments = productCommentService.getCommentsByProductId(productId);
        return ResultUtils.success(comments);
    }

    @GetMapping("/replies")
    public BaseResponse<List<ProductComment>> getCommentReplies(@RequestParam Long commentId) {
        List<ProductComment> replies = productCommentService.getCommentReplies(commentId);
        return ResultUtils.success(replies);
    }

    @PostMapping("/like")
    public BaseResponse<Boolean> likeComment(@RequestBody ProductComment comment) {
        productCommentService.likeComment(comment.getId());
        return ResultUtils.success(true);
    }

    @GetMapping("/user")
    public BaseResponse<List<ProductComment>> getUserComments(HttpServletRequest httpServletRequest) {
       User user = userService.getLoginUser(httpServletRequest);
        List<ProductComment> comments = productCommentService.getUserComments(user.getId());
        return ResultUtils.success(comments);
    }

    @GetMapping("/count")
    public BaseResponse<Long> getCommentCount(@RequestParam Long productId) {
        Long count = productCommentService.getCommentCount(productId);
        return ResultUtils.success(count);
    }

    @GetMapping("/rating")
    public BaseResponse<Double> getAverageRating(@RequestParam Long productId) {
        Double rating = productCommentService.getAverageRating(productId);
        return ResultUtils.success(rating);
    }
} 